#include "..\..\comm\AI8051U.h"
#include "stdio.h"
#include "intrins.h"
#include "LIS3DHTR.h"

sbit SDA = P2^3;
sbit SCL = P2^4;

#define SLAW        0x30
#define SLAR        0x31

	

void I2C_Init()
{
    I2C_S1 =0;      //I2C功能脚选择，00:P2.4,P2.3; 01:P1.5,P1.4; 11:P3.2,P3.3
    I2C_S0 =0;
    SDA = 1;
    SCL = 1;

    P2M0 = (P2M0 & ~0x08) | 0x10; 
    P2M1 &= ~0x18; 

    P2PU = (P2PU & ~0x10) | 0x08; 
    P2SR &= ~0x18; 

    I2CCFG = 0xc0;  //使能I2C主机模式
    I2CCFG |= 23;
    I2CPSCR = 0x00; //MSSPEED[13:6]
    I2CMSST = 0x00;
}

/********************** I2C函数 ************************/
void Wait()
{
    while (!(I2CMSST & 0x40));
    I2CMSST &= ~0x40;
}

void Start()
{
    I2CMSCR = 0x01;                         //发送START命令
    Wait();
}

void SendData(char dat)
{
    I2CTXD = dat;                           //写数据到数据缓冲区
    I2CMSCR = 0x02;                         //发送SEND命令
    Wait();
}

void RecvACK()
{
    I2CMSCR = 0x03;                         //发送读ACK命令
    Wait();
}

char RecvData()
{
    I2CMSCR = 0x04;                         //发送RECV命令
    Wait();
    return I2CRXD;
}

void SendACK()
{
    I2CMSST = 0x00;                         //设置ACK信号
    I2CMSCR = 0x05;                         //发送ACK命令
    Wait();
}

void SendNAK()
{
    I2CMSST = 0x01;                         //设置NAK信号
    I2CMSCR = 0x05;                         //发送ACK命令
    Wait();
}

void Stop()
{
    I2CMSCR = 0x06;                         //发送STOP命令
    Wait();
}

void WriteNbyte(unsigned char addr, unsigned char *p, unsigned char number)  /*  WordAddress,First Data Address,Byte lenth   */
{
    Start();                                //发送起始命令
    SendData(SLAW);                         //发送设备地址+写命令
    RecvACK();
    SendData(addr);                         //发送存储地址
    RecvACK();
    do
    {
        SendData(*p++);
        RecvACK();
    }
    while(--number);
    Stop();                                 //发送停止命令
}

void ReadNbyte(unsigned char  addr, unsigned char *p, unsigned char number)   /*  WordAddress,First Data Address,Byte lenth   */
{
    Start();                                //发送起始命令
    SendData(SLAW);                         //发送设备地址+写命令
    RecvACK();
    if(number > 1)
    {
       addr |= 0x80; 
    }
    SendData(addr);                         //发送存储地址
    RecvACK();
    Start();                                //发送起始命令
    SendData(SLAR);                         //发送设备地址+读命令
    RecvACK();
    do
    {
        *p = RecvData();
        p++;
        if(number != 1) SendACK();          //send ACK
    }
    while(--number);
    SendNAK();                              //send no ACK	
    Stop();                                 //发送停止命令
}

void LIS3DH_Init()
{
    char cmd = 0;
    ReadNbyte(LIS3DH_WHO_AM_I_REG,&cmd,1);
    if(cmd == 0x33)
    {//判断 器件  是否 通信 正常   
        cmd = 0x80; 
        WriteNbyte(LIS3DH_TEMP_CFG_REG, &cmd,1); // 打开 内部 温度 传感器  和 ADC 功能 
        cmd = 0x77; 
        WriteNbyte(LIS3DH_CTRL_REG1, &cmd,1); // 
        cmd = 0x88;
        WriteNbyte(LIS3DH_CTRL_REG4, &cmd,1); // 
        
    }
}

